﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Scap.Info;
using Scap.Data;

namespace Scap.Core
{
    public class EmployeeHandler
    {
        public ScapEmployeeCollection GetScapEmployees()
        {
            ScapEmployeeCollection coll = new ScapEmployeeCollection();

            using (ScapData db = new ScapData())
            {
                var records = from d in db.Employees select d;

                foreach (ScapEmployee r in records)
                {
                    coll.Add(r);
                }
            }

            return coll;
        }

        internal ScapEmployee GetScapEmployee(int id)
        {
            using (ScapData db = new ScapData())
            {
                ScapEmployee record = (from d in db.Employees where d.Id == id select d).FirstOrDefault();

                return record;
            }
        }

        public ScapEmployeeEnroll GetEmployeeEnroll(int employeeId, int index)
        {
            using (ScapData db = new ScapData())
            {
                ScapEmployeeEnroll record = (from d in db.EmployeeEnrolls where d.EmployeeId == employeeId && d.Index == index select d).FirstOrDefault();

                return record;
            }
        }

        public ScapEmployeeEnrollCollection GetEmployeeEnrollments(int employeeId)
        {
            ScapEmployeeEnrollCollection coll = new ScapEmployeeEnrollCollection();

            using (ScapData db = new ScapData())
            {
                var records = from d in db.EmployeeEnrolls where d.EmployeeId == employeeId  select d;

                foreach (ScapEmployeeEnroll r in records)
                {
                    coll.Add(r);
                }
            }

            return coll;
        }

        public void Save(ScapEmployee employee, ScapEmployeeEnroll enroll)
        {
            ScapData db = new ScapData();
            enroll.EmployeeId = employee.Id;
            ScapEmployeeEnroll target = GetEmployeeEnroll(enroll.EmployeeId, enroll.Index);


            if (target == null)
            {
                target = enroll;
                db.Entry(target).State = System.Data.EntityState.Added;
            }
            else
            {
                target.Data = enroll.Data;
                target.DataStr = enroll.DataStr;
                db.Entry(target).State = System.Data.EntityState.Modified;
            }


            // update to database
            
            db.Entry(employee).State = System.Data.EntityState.Modified;
            
            // update to devices      

            ScapCommand cmd = new ScapCommand();
            cmd.Command = CommandType.PUT_EMPLOYEE_ON_DEVICES;
            cmd.EmployeeId = employee.Id;
            cmd.Index = enroll.Index;
            db.Entry(cmd).State = System.Data.EntityState.Added;

            db.SaveChanges();
        }

       
    }
}
